package vn.fpt.fsoft.dao;

import java.util.*;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import vn.fpt.fsoft.entity.*;
import vn.fpt.fsoft.util.HibernateUtil;

/*
 * Class GovOfficeRegionDAO
 *
 * Version 1.0
 *
 * 22-August-2014
 *
 * Modification Logs:
 * DATE                 AUTHOR              DESCRIPTION
 * --------------------------------------------------------
 * 22-August-2014       NguyenTuyen          Create GovRegionDAO
 */
public class GovOfficeRegionDAO {
    
    /**
     * Session.
     */
    private Session session;
    
    /**
     * Constructor Government office region DAO.
     */
    public GovOfficeRegionDAO() {
        this.session = HibernateUtil.getSessionFactory().openSession();
    }
    
    /**
     * Government Office Region.
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<GovOfficeRegion> getAllGovOfficeRegion() {
        List<GovOfficeRegion> ds = null;
        try {
            session.beginTransaction();
            Criteria cri = session.createCriteria(GovOfficeRegion.class);
            ds = cri.list();
            session.getTransaction().commit();
        } catch (HibernateException ex) {
            if (session.getTransaction() != null) {
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        } finally {
            session.close();
        }
        return ds;
    }
    
    /**
     * Create a new Government Office Region.
     * 
     * @param gov
     * @return
     */
    public boolean addGovOfficeRegion(GovOfficeRegion gov) {
        try {
            session.beginTransaction();
            session.save(gov);
            session.getTransaction().commit();
            return true;
        } catch (HibernateException e) {
            if (session.getTransaction() != null) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return false;
    }
    
    /**
     * Update a Government Office Region.
     * 
     * @param gov
     * @return
     */
    public boolean updateGovOfficeRegion(GovOfficeRegion gov) {
        try {
            session.beginTransaction();
            session.update(gov);
            session.getTransaction().commit();
            return true;
        } catch (HibernateException e) {
            if (session.getTransaction() != null) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return false;
    }
    
    /**
     * Get a Government Office Region with govOfficeRegionID.
     * 
     * @param govOfficeRegionID
     * @return
     */
    public GovOfficeRegion getGovOfficeRegionByID(int govOfficeRegionID) {
        GovOfficeRegion gov = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            gov = (GovOfficeRegion) session.get(GovOfficeRegion.class,
                    govOfficeRegionID);
            session.getTransaction().commit();
            return gov;
        } catch (HibernateException e) {
            if (session.getTransaction() != null) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return gov;
    }
    
    /**
     * Get list Government Office Region with Government's name.
     * 
     * @param govOfficeRegionName
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<GovOfficeRegion> getGovOfficeRegionByName(
            String govOfficeRegionName) {
        List<GovOfficeRegion> ds = null;
        try {
            session.beginTransaction();
            Criteria cri = session
                    .createCriteria(GovOfficeRegion.class)
                    .add(Restrictions.eq("govOfficeRegionName",
                            govOfficeRegionName))
                    .add(Restrictions.eq("isActive", true));
            ds = cri.list();
            session.getTransaction().commit();
        } catch (HibernateException ex) {
            if (session.getTransaction() != null) {
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        } finally {
            session.close();
        }
        return ds;
    }
}
